import { ModelTemplate, ControllerTemplate, ServiceTemplate, RouterTemplate } from '@/configs';
import { LineBreak, ProjectRoot } from '@/constant';
import { Ifilling, IModuleType, IOptions } from '@/types';
import { upperFirstStrCase } from '@/util';
import { writeFile, removeFile, isFile } from '@/util/files';

const catalogue = ProjectRoot['Node Server'];

const normalize = (document: IModuleType, infilling: Ifilling) => {

    const database = infilling;

    if (document === 'Package.json') writeFile(`${catalogue}/${document.toLowerCase()}`, infilling);
    
    if (document === 'Model') {
        // index.ts
        writeFile(
            `${catalogue}/src/models/index.ts`,
            [
                database.table.map((table) => `${LineBreak}  ${upperFirstStrCase(table.name)},`),
                database.table.map((table) => `import ${upperFirstStrCase(table.name)} from './module/${table.name.toLowerCase()}'${LineBreak}`)
            ],
            [
                /(const Models = {)/g,
                /()/,
            ]
        );
        // modules/*
        database.table.forEach((table) => {
            writeFile(`${catalogue}/src/models/module/${table.name}.ts`, ModelTemplate(table));
        });
    }

    if (document === 'Service') {
        // index.ts
        writeFile(
            `${catalogue}/src/services/index.ts`,
            [
                database.table.map((table) => `${LineBreak}  ${upperFirstStrCase(table.name)},`),
                database.table.map((table) => `import ${upperFirstStrCase(table.name)} from './module/${table.name.toLowerCase()}'${LineBreak}`)
            ],
            [
                /(const Services = {)/g,
                /()/,
            ]
        );
        // modules/*
        database.table.forEach((table) => {
            writeFile(`${catalogue}/src/services/module/${table.name}.ts`, ServiceTemplate(table));
        });
    }

    if (document === 'Controller') {
        // index.ts
        writeFile(
            `${catalogue}/src/controllers/index.ts`,
            [
                database.table.map((table) => `${LineBreak}  ${upperFirstStrCase(table.name)},`),
                database.table.map((table) => `import ${upperFirstStrCase(table.name)} from './module/${table.name.toLowerCase()}'${LineBreak}`)
            ],
            [
                /(export default {)/g,
                /()/,
            ]
        );
        // modules/*
        database.table.forEach((table) => {
            writeFile(`${catalogue}/src/controllers/module/${table.name}.ts`, ControllerTemplate(table));
        });
    }

    if (document === 'Config') {
        writeFile(
            `${catalogue}/src/config/mysqlBase.ts`,
            [
                [`"${database.name}"`],
                [`"${database.username}"`],
                [`"${database.password}"`],
                [`"${database.ip}"`],
            ],
            [
                /mysqlName: ([\'\"]Database Name[\'\"])/g,
                /mysqlUserName: ([\'\"]Username[\'\"])/g,
                /mysqlPassword: ([\'\"]Password[\'\"])/g,
                /mysqlIp: ([\'\"]localhost[\'\"])/g,
            ]
        );
    }

    if (document === 'Route') {
        writeFile(
            `${catalogue}/src/routes/index.ts`,
            [ database.table.map((table) => RouterTemplate(table)) ],
            [ /(const config: IConfig\[\] = \[)/g ]
        );
    }

};

const removeOldVersion = (path) => {
    if (isFile(path) !== null) {
        // 存在旧版本的文件或文件夹, 删除
        removeFile(path);
    }
};

const deleteUseless = () => {
    // should delete everything that "example" relatived.
    removeFile(`${catalogue}/src/controllers/module/example.ts`);
    removeFile(`${catalogue}/src/models/module/example.ts`);
    removeFile(`${catalogue}/src/services/module/example.ts`);
};

const basePreSolve = (options: IOptions) => {
    // 需要加入对字段缺失的判断和提示
    const { database, ...packages } = options;
    normalize('Package.json', packages);
    normalize('Model', database);
    normalize('Service', database);
    normalize('Controller', database);
    normalize('Config', database);
    normalize('Route', database);
    // deleteUseless();
};

export {
    deleteUseless,
    removeOldVersion,
    basePreSolve,
}